package chapter11;

import org.junit.Test;

import java.util.*;

/**
 * @author : DuJiabao
 * @Date : 2021/2/19 22:38
 * @Project : thinking-in-java
 * @File : null.java
 * @Desc :
 */
public class CollectionTest {
  public static <T> void display(Iterator<T> iterator) {
    while (iterator.hasNext()) {
      System.out.println(iterator.next());
    }
  }

  public static <T> void display(Collection<T> collection) {
    for (T c : collection) {
      System.out.println(c);
    }
  }

  @Test
  public void test() {
    Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5));
    List<String> list = new ArrayList<String>(Arrays.asList("fa fdaf af et".split(" ")));
    Map<Integer, Integer> map = new HashMap<>();
    Random rand = new Random(47);
    for (int i = 0; i < 10000; i++) {
      int r = rand.nextInt(20);
      Integer freq = map.get(r);
      map.put(r, freq == null ? 1 : freq + 1);
    }

    display(set);
    display(set.iterator());
    display(list);
    display(list.iterator());
    display(map.keySet());
    display(map.keySet().iterator());
    display(map.values());
    display(map.values().iterator());
    display(map.entrySet());
    display(map.entrySet().iterator());
  }
}




















